package controller;

import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.QuestionModel;
import entity.Question;

/**
 * Servlet implementation class GardenQuestion
 */
@WebServlet("/GardenQuestion")
public class GardenQuestion extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// Language
		HttpSession session = request.getSession(true);
		String lang = "";
		ResourceBundle resource = null;
		if (session.getAttribute("language") != null) {
			resource = (ResourceBundle) session.getAttribute("language");
			lang = resource.getString("code");

		} else {
			resource = ResourceBundle.getBundle("en", new Locale("en"));
			session.setAttribute("language", resource);
			lang = "en";
		}

		// Property handle
		request.setAttribute("title", resource.getString("title"));
		request.setAttribute("keyword", resource.getString("keyword"));
		request.setAttribute("description", resource.getString("description"));

		// Return view
		QuestionModel questionModel = new QuestionModel();
		List<Question> listQuestion = questionModel.all(lang, 0);

		if (listQuestion.size() > 0) {
			request.setAttribute("currentPage", 0);
			request.setAttribute("listQuestion", listQuestion);
			request.getRequestDispatcher("/index.jsp").forward(request,
					response);
		} else {
			request.getRequestDispatcher("/erro.jsp")
					.forward(request, response);
		}
	}

}
